from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support import wait
import time

from selenium.webdriver.support.select import Select

from com.wzh.mile.unit_test.common.bank_card_generator import BankCardGenerator
from com.wzh.mile.unit_test.common.chinese_generator import ChineseGenerator
from com.wzh.mile.unit_test.common.mydb import MyDB

gen = BankCardGenerator().generate_card_no()
chinese = ChineseGenerator.get_char()
phone_no = 18200001107
id_card = gen.generate_id_card()
print(id_card)
real_name = chinese.get_char()
print(real_name)
browser = webdriver.Firefox()

browser.get('http://192.168.1.208:8085//')
#点击红包
red = browser.find_element_by_id('red-box')
red.click()
browser.find_element_by_link_text('取消').click()
#滚动到底部
browser.execute_script("document.documentElement.scrollTop=10000")
browser.implicitly_wait(3)
#隐藏红包 不然点击不了下边的关闭
browser.execute_script("document.getElementById('red-box').hidden = true")
#关闭广告
ad = browser.find_element_by_class_name('close-box')
if ad.is_enabled():
    ad.click()

wode = browser.find_element_by_link_text('我的')
wode.click()

browser.execute_script("document.documentElement.scrollTop=10000")

browser.implicitly_wait(5)

zhuce = browser.find_element_by_id('go-register')
zhuce.click()

tel = browser.find_element_by_id('username')
tel.send_keys(phone_no)

password = browser.find_element_by_id('password')
password.send_keys('abcd1234')

EC.visibility_of_all_elements_located
tuxm =browser.find_element_by_id('checkcode')
while True:
    val = browser.find_element_by_id('checkcode').get_attribute('value')
    if val and len(val) >= 4:
        break
    else:
        print('等待数字短信验证码。。。')
        time.sleep(2)

yanzm = browser.find_element_by_id('JS-regpanel')
EC.element_to_be_clickable(browser.find_element_by_id('JS-regpanel'))
yanzm = browser.find_element_by_id('JS-regpanel')
yanzm.click()

EC.element_to_be_clickable(browser.find_element_by_class_name('weui_btn_dialog'))
chongzhi_tanchuang = browser.find_element_by_class_name('weui_btn_dialog')
chongzhi_tanchuang.click()
browser.implicitly_wait(5)

while True:
    val = browser.find_element_by_id('mobilecode').get_attribute('value')
    if val and len(val) == 4:
        break
    else:
        print('等待手机短信验证码。。。')
        mobilecode = MyDB().select_by_id(phone_no)
        if mobilecode:
            browser.find_element_by_id('mobilecode').send_keys(mobilecode)
        time.sleep(2)
pass


next = browser.find_element_by_id('btnRegister')
next.click()

EC.element_to_be_clickable(browser.find_element_by_class_name('weui_btn_dialog '))
# zhuce_queding = browser.find_element_by_class_name('weui_btn_dialog')
# zhuce_queding.click()
browser.execute_script("$('.weui_btn_dialog').click()")
#页面跳转时

var = EC.visibility_of_all_elements_located


#实名认证
#填入姓名
name =browser.find_element_by_id('realName')
name.send_keys(real_name)
#填入身份证

cardnumber=browser.find_element_by_id('cardNumber')
cardnumber.send_keys(id_card)
#点击认证按钮
register = browser.find_element_by_class_name('btn-register')
register.click()
#注册成功点击确定按钮
EC.element_to_be_clickable(browser.find_element_by_class_name('weui_btn_dialog '))
EC.presence_of_all_elements_located
time.sleep(2)
zhuce_jieguo = browser.find_element_by_class_name('weui_dialog_bd').text
zhuce_queding = browser.find_element_by_class_name('weui_btn_dialog')
zhuce_queding.click()

if  zhuce_jieguo != '提交成功':
    name = browser.find_element_by_id('realName')
    real_name2 = chinese.get_char()
    name.send_keys(real_name2)
    card_no2 = gen.generate_id_card()
    cardnumber = browser.find_element_by_id('cardNumber')
    cardnumber.send_keys(card_no2)
    EC.element_to_be_clickable(browser.find_element_by_id('subfrm'))
    browser.implicitly_wait(3)
    browser.find_element_by_id('subfrm').click()
    #注册成功点击确定按钮
    EC.element_to_be_clickable(browser.find_element_by_class_name('weui_btn_dialog '))
    EC.presence_of_all_elements_located
    time.sleep(2)
    zhuce_jieguo = browser.find_element_by_class_name('weui_dialog_bd').text
    zhuce_queding = browser.find_element_by_class_name('weui_btn_dialog')
    zhuce_queding.click()

#新浪支付认证手机
time.sleep(5)
EC.visibility_of_all_elements_located
EC.presence_of_all_elements_located
EC.presence_of_element_located(browser.find_element_by_name('newMobile'))
xinlang_mobile =browser.find_element_by_name('newMobile')
browser.execute_script("document.getElementById('tela').value='' ")
xinlang_mobile.send_keys('18248781879')

#实名认证

# 输入姓名
# 输入密码


#验证码
browser.implicitly_wait(5)

EC.element_to_be_clickable(browser.find_element_by_class_name('get_check_code'))
chongzhi_tanchuang = browser.find_element_by_class_name('get_check_code')
chongzhi_tanchuang.click()

while True:
    val = browser.find_element_by_id('J-sms').get_attribute('value')
    if val and len(val) == 6:
        break
    else:
        # if browser.find_element_by_class_name('get_check_code').is_enabled():
        #     browser.find_element_by_class_name('get_check_code').click()
        print('等待手机短信验证码。。。')
        time.sleep(2)
pass

#密码
xinlang_passwd =browser.find_element_by_id('J-psw')
xinlang_passwd.send_keys('abcd@1234')

#确认密码

check_passwd=browser.find_element_by_id('J-check_psw')
check_passwd.send_keys('abcd@1234')

#完成
Determine = browser.find_element_by_class_name('btn-danger')
Determine.click()
time.sleep(2)

EC.element_to_be_clickable(browser.find_element_by_id('cz-btn'))
browser.find_element_by_id('cz-btn').click()

EC.element_to_be_clickable(browser.find_element_by_link_text('立即绑定'))
browser.find_element_by_link_text('立即绑定').click()

browser.implicitly_wait(5)
EC.element_to_be_clickable(browser.find_element_by_class_name('crash_card '))
browser.find_element_by_class_name('crash_card ')

EC.presence_of_all_elements_located

crash_card = browser.find_element_by_class_name('crash_card ')
crash_card
# 添加银行卡
#卡号
bankCardNumber =browser.find_element_by_id('bankCardNumber')
bankCardNumber.send_keys('6217008454414258074')

# # 开户行
# bankCard = browser.find_element_by_id('bankCard')
# card = bankCard.find_element_by_tag_name('option')
# for option in card:
#     print("value")
#     option.click()
select = Select(browser.find_element_by_id('bankCard'))
select.deselect_all()
select.select_by_visible_text('建设银行')

# 开户支行
bankDetail = browser.find_element_by_id('bankDetail')
bankDetail.send_keys('test')

# 选择省市
# 预留手机号